const HttpException = require('../exception/http.exception.js');
const User = require('../model/user');
const Comment = require('../model/comment');
const sequelize = require('../db/sequelize.js');
const Article = require('../model/article.js');

//创建评论
module.exports.createComment = async (req, res, next) => {
    try {
        //获取文章s1ug
        const { slug } = req.params
        //获取评论内容
        const { body } = req.body
        console.log("back",body);
        //获取评论文章
        const article = await Article.findByPk(slug)
        if (!article) {
            throw new HttpException(404, '评论不存在', 'comments not found')
        }
        //获取评论用户
        const user = await User.findByPk(req.user.email)
        if (!user) {
            throw new HttpException(404, '用户不存在', 'user not found')
        }
        //创建评论
        //01 存储评论数据
        let newComment = await Comment.create({ body })
        //02 存储用户和评论关系
        await user.addComments(newComment)
        //03 存储文章和评论关系
        await article.addComments(newComment)
        //追加：评论人:newComment
        newComment.dataValues.user = {
            username: user.username,
            avatar: user.avatar,
            bio: user.bio
        }
        console.log(newComment);
        //返回数据
        return res.status(200)
            .json({
                status: 1,
                message: "创建评论成功",
                data: newComment
            })
    } catch (error) {
        next(error)
    }
}

//获取评论列表
module.exports.getComments = async (req, res, next) => {
    try {
        //获取slug
        const { slug } = req.params
        //通过slug获取article
        const article = await Article.findByPk(slug)
        //判断文章是否存在
        if (!article) {
            throw new HttpException(404, '文章不存在', 'Article does not exist')
        }
        //获取评论:条件 某个文章/评论人信息
        const comments = await Comment.findAll({
            where: {
                articleSlug: slug
            },
            include: [
                {
                    model: User,
                    attributes: ['username', 'bio', 'avatar']
                }
            ]
        })
        return res.status(200)
            .json({
                status: 1,
                message: "获取评论成功",
                data: comments
            })
    } catch (error) {
        next(error)
    }
}

//删除评论
module.exports.deleteComment = async (req, res, next) => {
    try {
        //获取slug和id
        const { slug, id } = req.params
        //验证article和comment是否存在
        const article = await Article.findByPk(slug)
        if (!article) {
            throw new HttpException(404, '文章不存在', 'Article does not exist')
        }
        const comment = await Comment.findByPk(id)
        if (!comment) {
            throw new HttpException(404, '文章不存在', 'Comment does not exist')
        }
        //当前登录用户是否是评论是当前评论的作者
        const userEmail = req.user.email
        const commentAuthorEmail = comment.userEmail
        if (userEmail !== commentAuthorEmail) {
            throw new HttpException(404, '当前登录用户不是该评论作者', 'The currently logged in user is not the author of this comment')
        }
        // console.log(userEmail, commentAuthorEmail);
        //删除评论
        const deleteComment = await comment.destroy()
        // const deleteComment=await Comment.destroy({where:{id}})   //使用数据库的方式 要加条件
        // console.log(comment);
        return res.status(200)
            .json({
                status: 1,
                message: "删除评论成功",
                data: deleteComment
            })
    } catch (error) {
        next(error)
    }
}